
const passport = require('koa-passport');

const unAuth = [
  '/v1/login',
  '/v1/logout',
];

module.exports = async (ctx, next) => {
  const path = ctx.request.path;
  // 无需登录校验
  if (unAuth.indexOf(path) > -1) {
    await next();
    return;
  }

  //登录校验
  if(ctx.isAuthenticated()) {
    ctx.user = ctx.session.passport.user;
    await next();
  } else {
    ctx.body = {
      code: -4,
      msg: 'Not Authenticated',
      data: null,
    };
  }
} 

